package com.jihong.util;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.List;

public class EncryptHandler {

    private static String k = "_&^&^*****";
    static String key = "1234567890123456"; // 16字节密钥
    static String iv = "1234567890123456";  // 16字节IV
    static Cipher cipher;
    static SecretKeySpec keySpec;
    static IvParameterSpec ivSpec;
    static {
        try {
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
        keySpec = new SecretKeySpec(key.getBytes(),  "AES");
        ivSpec = new IvParameterSpec(iv.getBytes());
    }





    public static String encryptColumn(String column){
        if (column.contains("`")){
            return column.substring(0,column.length() - 1) + "_en`";
        }
        return column + "_en";
    }

    public static String decryptColumn(String column){
        return column.replace("_en","");
    }

    public static Object encryptField(Object field){
        return field + "_aes_en";
    }

    public static String encryptValue(String value){
        try {

            return value + k;
//            cipher.init(Cipher.ENCRYPT_MODE,  keySpec, ivSpec);
//            byte[] encrypted = cipher.doFinal(value.getBytes());
//            return Base64.getEncoder().encodeToString(encrypted);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static String decryptValue(String value){
        try {
            return value.replace(k,"");
//            cipher.init(Cipher.DECRYPT_MODE,  keySpec, ivSpec);
//            byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(value));
//            return new String(decrypted);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
//        String qqada = encryptValue("qqada");
//        String s = decryptValue(qqada);
//        System.out.println(s);
        String str = "abcd";
        System.out.println();
    }


}